﻿
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using MyWeb.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyWeb.Common;
using MyWeb.Business;
namespace MyWeb.Admins
{
    public partial class tblOrderDetail : System.Web.UI.Page
    {
        static bool Insert = false;protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack){Session["folder"] = "tblOrderDetail";
                BindGrid();
            }
        }
        private void BindGrid()
        {
            grdtblOrderDetail.DataSource = tblOrderDetailService.tblOrderDetail_GetByAll() ;
            grdtblOrderDetail.DataBind();
        }
        protected void btnThem_Click(object sender, EventArgs e)
        {
            pnShow.Visible = false;
            pnUpdate.Visible = true;
            ControlClass.ResetControlValues(this);
            Session["upload"] = null;Insert=true;
        }
        protected void btnXoa_Click(object sender, EventArgs e)
        {
            try
            {
                DataGridItem item = default(DataGridItem);
                for (int i = 0; i < grdtblOrderDetail.Items.Count; i++)
                {
                    item = grdtblOrderDetail.Items[i];
                    if (item.ItemType == ListItemType.AlternatingItem | item.ItemType == ListItemType.Item)
                    {
                        if (((CheckBox)item.FindControl("ChkSelect")).Checked)
                        {
                            string strId = item.Cells[1].Text;
                            try
                            {
                                tblOrderDetailService.tblOrderDetail_Delete(strId);
                            }
                            catch
                            {
                            }
                        }
                    }
                }
                grdtblOrderDetail.CurrentPageIndex = 0;
                BindGrid();
            }
            catch
            {
            }
        }
        protected void grdtblOrderDetail_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            Data.tblOrderDetailInfo strCA=new tblOrderDetailInfo();
            strCA._odid = e.CommandArgument.ToString();
            if  (e.CommandName=="Edit")
            {
                DataTable dt = tblOrderDetailService.tblOrderDetail_GetById(strCA);
                dll_imgid_Update.SelectedValue=dt.Rows[0]["_imgid"].ToString();
                txt_oid.Text=dt.Rows[0]["_oid"].ToString();
                txt_odid.Text=dt.Rows[0]["_odid"].ToString();
                txt_price.Text=dt.Rows[0]["_price"].ToString();
                txt_quantity.Text=dt.Rows[0]["_quantity"].ToString();
                pnUpdate.Visible = true;
                pnShow.Visible = false;
            }
            else
            {
                tblOrderDetailService.tblOrderDetail_Delete(strCA._odid);
                BindGrid();
            }
        }
        protected void lblBack_Click(object sender, EventArgs e)
        {
            pnUpdate.Visible = false;
            pnShow.Visible = true;
            Insert=false;
        }
        protected void lblUpdate_Click(object sender, EventArgs e)
        {
            string image = Session["upload"] == null ? "" : Session["upload"].ToString();
    #region[TestInput]
            if (dll_imgid_Update.SelectedValue.Trim().Equals(""))
            {
                WebMsgBox.Show("_imgid không thể trống !");
                dll_imgid_Update.Focus();
                return;
            }
            if (txt_oid.Text.Trim().Equals(""))
            {
                WebMsgBox.Show("_oid không thể trống !");
                txt_oid.Focus();
                return;
            }
            if (txt_odid.Text.Trim().Equals(""))
            {
                WebMsgBox.Show("_odid không thể trống !");
                txt_odid.Focus();
                return;
            }
            if (txt_price.Text.Trim().Equals(""))
            {
                WebMsgBox.Show("_price không thể trống !");
                txt_price.Focus();
                return;
            }
            if (txt_quantity.Text.Trim().Equals(""))
            {
                WebMsgBox.Show("_quantity không thể trống !");
                txt_quantity.Focus();
                return;
            }

#endregion
            Data.tblOrderDetailInfo obj=new Data.tblOrderDetailInfo();
            obj._imgid = dll_imgid_Update.SelectedValue;
            obj._oid = txt_oid.Text;
            obj._odid = txt_odid.Text;
            obj._price = txt_price.Text;
            obj._quantity = txt_quantity.Text;
            if (Insert == true)
            {
                tblOrderDetailService.tblOrderDetail_Insert(obj);
            }
            else
            {
                tblOrderDetailService.tblOrderDetail_Update(obj);
            }
            BindGrid();
            pnShow.Visible = true;
            pnUpdate.Visible = false;
            Insert = false;
        }
        protected void grdtblOrderDetail_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            grdtblOrderDetail.CurrentPageIndex = e.NewPageIndex;
            BindGrid();
        }
        private void ShowDll()
        {
            var dt = new DataTable();
            dt = tblImagesService.tblImages_GetByAll();
            dll_imgid.DataSource = dt;
            dll_imgid.DataTextField = "";
            dll_imgid.DataValueField = "";
            dll_imgid.DataBind();
            dll_imgid_Update.DataSource = dt;
            dll_imgid_Update.DataTextField = "";
            dll_imgid_Update.DataValueField = "";
            dll_imgid_Update.DataBind();
        }
        protected void btnshow_Click(object sender, EventArgs e)
        {
            grdtblOrderDetail.DataSource =tblOrderDetailService.tblOrderDetail_GetBy_imgid(dll_imgid.SelectedValue);
            grdtblOrderDetail.DataBind();
        }
        protected void grdtblOrderDetail_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            ListItemType itemType = e.Item.ItemType;
            if ((itemType != ListItemType.Footer) && (itemType != ListItemType.Separator))
            {
                if (itemType == ListItemType.Header)
                {
                    object checkBox = e.Item.FindControl("chkSelectAll");
                    if ((checkBox != null))
                    {
                        ((CheckBox)checkBox).Attributes.Add("onClick", "Javascript:chkSelectAll_OnClick(this)");
                    }
                }
                else
                {
                    string tableRowId = grdtblOrderDetail.ClientID + "_row" + e.Item.ItemIndex.ToString();
                    e.Item.Attributes.Add("id", tableRowId);
                    object checkBox = e.Item.FindControl("chkSelect");
                    if ((checkBox != null))
                    {
                        e.Item.Attributes.Add("onMouseMove", "Javascript:chkSelect_OnMouseMove(this)");
                        e.Item.Attributes.Add("onMouseOut", "Javascript:chkSelect_OnMouseOut(this," + e.Item.ItemIndex + ")");
                        ((CheckBox)checkBox).Attributes.Add("onClick", "Javascript:chkSelect_OnClick(this," + e.Item.ItemIndex + ")");
                    }
                }
            }
        }
    }
}